package com.example.redistest.webcontroller;

import com.example.redistest.redislocktest.CommonLockResourceService;
import com.example.redistest.redislocktest.testclass.SysPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.transform.Result;
import java.util.Date;

/**
 * \* @author: wky
 * \* Date: 2023-04-20
 * \* Time: 15:18
 * \* To change this template use File | Settings | File Templates.
 * \* Description:SysPlanController
 * \
 */
@RestController
@RequestMapping("/sysplan")
public class SysPlanController {

    static final String lockKeyPrefix = "lock_plan_";

    @Autowired
    private CommonLockResourceService redisService;

    @GetMapping("/getbyidlock")
    private Object getByIdLock(@RequestParam int planId,@RequestParam int userId) {
        boolean isLock = redisService.getLock(lockKeyPrefix,planId,userId);
        if(isLock) {
            SysPlanService planService = new SysPlanService();
            planService.setId(1);
            planService.setBranchOfficeId(planId);
            planService.setHeadOfficePlan("head office plan");
            planService.setBranchOfficePlan("branch office plan");
            planService.setCreateDate(new Date());
            planService.setUpdateTime(new Date());
            return planService;
        }
        return "当前数据正在编辑中，请稍候重试!";
    }

    @GetMapping("/update")
    public Object update(@RequestParam int planId,@RequestParam int userId) {
        boolean isRelease = redisService.unLock(lockKeyPrefix,planId,userId);
        return isRelease?"释放锁成功":"释放锁失败";
    }

    public Object resetLock(@RequestParam int planId,@RequestParam int userId) {
        boolean success = redisService.resetLock(lockKeyPrefix, planId,userId);
        return success?"释放锁成功":"释放锁失败";
    }


}